package com.rdc.qingkou.presenter;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.rdc.qingkou.base.BasePresenterImpl;
import com.rdc.qingkou.contract.PlayContract;
import com.rdc.qingkou.ui.PlayDialogActivity;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Timer;
import java.util.TimerTask;


public class PlayPresenter extends BasePresenterImpl<PlayContract.View> implements PlayContract.PresenterI {
    private Timer timer ;
    private int mTime = 0;
    private MediaPlayer mMediapalyer;
    private String audioFile;
    private Bundle bundle = new Bundle();
    private Message mMaxMessage ;
    private Message mMinMessage;
    private  Handler mHandler;
    private int mMaxTime;
    private static final String TAG = "PlayPresenter";

    @Override
    public void play() {

        mView.setRecord("正在播放...");
        mHandler = new PlayHandler((PlayDialogActivity) mView);
           audioFile = mView.getPath();
        try{
            mView.setRecord("正在播放...");
                mMediapalyer = new MediaPlayer();
                mMediapalyer.setDataSource(audioFile);
                mMediapalyer.prepare();
                mMediapalyer.start();
                mMaxTime = mMediapalyer.getDuration();
            }catch (IOException e){
                e.printStackTrace();
            }

             mTime = 0;
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {

                        if (mTime > 120) {

                            timer.cancel();
                        } else {
                            if (mTime*1000 < mMaxTime){

                                bundle.clear();
                                bundle.putInt("time",mTime*1000*120000/mMaxTime);
                                mMaxMessage = new Message();
                                mMaxMessage.setData(bundle);

                                mHandler.sendMessage(mMaxMessage);
                             }else {
                                bundle.clear();
                                bundle.putInt("time",-1);
                                mMinMessage = new Message();
                                mMinMessage.setData(bundle);
                                mHandler.sendMessage(mMinMessage);
                            }

                        }
                    mTime++;

                }
            },0,1000);

    }

    @Override
    public void stop() {
        if (timer!=null){
            timer.cancel();
            timer = null;
        }
        if (mMediapalyer!=null){

            mMediapalyer.release();
            mMediapalyer = null;
        }


    }

    private static  class PlayHandler extends  Handler {
        private  final WeakReference<PlayDialogActivity> mActivity;

        public PlayHandler(PlayDialogActivity activity) {
            mActivity = new WeakReference<PlayDialogActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            PlayDialogActivity activity = mActivity.get();
            if(activity!=null){
                int i = msg.getData().getInt("time");
                if (i>=0){
                    activity.setProgressBar(i);
                }else {
                    activity.getInstance().stop();
                    activity.setProgressBar(0);
                    activity.setRecord("点击试听");

                }

            }
            }
        }
    }




